package com.smalls.newvideotwo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.db.Album;
import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.utils.ImageLoaderUtil;
import com.smalls.newvideotwo.utils.MeasureUtil;
import com.smalls.newvideotwo.utils.Tools;
import com.smalls.newvideotwo.utils.Utils;
import com.smalls.newvideotwo.view.BaseGridView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class VodtypeAdapter extends BaseAdapter {
    private Context context;
    private String curreDate;
    private ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil();
    private LayoutInflater mInflater;
    public List<Album> vodDatas;
    private String vodType;

    static class ViewHolder {
        private FrameLayout broader;
        private ImageView poster;
        private TextView videoName;

        ViewHolder() {
        }
    }

    public VodtypeAdapter(Context context, ArrayList<Album> datas, String vodType) {
        this.context = context;
        this.vodDatas = datas;
        this.vodType = vodType;
        this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void changData(ArrayList<Album> paramArrayList) {
        setVideos(paramArrayList);
        notifyDataSetChanged();
    }

    private void setVideos(ArrayList<Album> paramArrayList) {
        if (paramArrayList != null) {
            this.vodDatas = paramArrayList;
        } else {
            this.vodDatas = new ArrayList();
        }
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mInflater.inflate(R.layout.movetype_details_item, null);
            holder = new ViewHolder();
            holder.poster = (ImageView) convertView.findViewById(R.id.video_poster);
            holder.videoName = (TextView) convertView.findViewById(R.id.video_name);
            holder.broader = (FrameLayout) convertView.findViewById(R.id.broader);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Album vd = this.vodDatas.get(position);
        holder.poster.setImageResource(R.color.transparent);
        this.imageLoaderUtil.loadImage(vd.getAlbumPic(),holder.poster, this.context, R.mipmap.default_poster, 5.0f);
        if (!TextUtils.isEmpty(vd.getTitle())) {
            holder.videoName.setText(vd.getTitle());
        }
        return convertView;
    }

    @SuppressLint({"SimpleDateFormat"})
    public boolean twoDateDistance(String compreeDate) {
        if (TextUtils.isEmpty(compreeDate)) {
            return false;
        }
        if (TextUtils.isEmpty(this.curreDate)) {
            this.curreDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = null;
        Date d2 = null;
        try {
            d1 = sdf.parse(this.curreDate);
            d2 = sdf.parse(compreeDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            if (Math.abs((d1.getTime() - d2.getTime()) / 86400000L) <= 7) {
                return true;
            }
            return false;
        } catch (Exception e2) {
            return false;
        }
    }

    public void clearData() {
        this.vodDatas.clear();
        notifyDataSetChanged();
    }

    public int getCount() {
        return this.vodDatas.size();
    }

    public Object getItem(int position) {
        return this.vodDatas.get(position);
    }

    public long getItemId(int position) {
        return (long) position;
    }
}
